<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>图解JS数组的forEach、map、find、reduce、filter方法 | Jayken (谢小舜)|谢小舜的个人博客</title>
    <meta name="description" content=" Jayken（谢小舜）的个人主页，记录我的程序员职业成长之路，分享前端、Java踩过的坑！">
    
    
    <link rel="preload" href="/assets/css/0.styles.0c98b5d4.css" as="style"><link rel="preload" href="/assets/js/app.2d175a42.js" as="script"><link rel="preload" href="/assets/js/2.af4947b8.js" as="script"><link rel="preload" href="/assets/js/7.f9e4b783.js" as="script"><link rel="prefetch" href="/assets/js/10.ead388bb.js"><link rel="prefetch" href="/assets/js/11.c26367e4.js"><link rel="prefetch" href="/assets/js/12.e15eef2b.js"><link rel="prefetch" href="/assets/js/13.71248d26.js"><link rel="prefetch" href="/assets/js/14.caa891a6.js"><link rel="prefetch" href="/assets/js/15.dba29244.js"><link rel="prefetch" href="/assets/js/3.8c0cb32d.js"><link rel="prefetch" href="/assets/js/4.ea6b297e.js"><link rel="prefetch" href="/assets/js/5.e528d392.js"><link rel="prefetch" href="/assets/js/6.d3c0fe75.js"><link rel="prefetch" href="/assets/js/8.cfbdc4b7.js"><link rel="prefetch" href="/assets/js/9.e68d7ec2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0c98b5d4.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="navbar-wrap"><div><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/logo_z.gif" alt="Jayken (谢小舜)|谢小舜的个人博客" class="logo"> <!----></a> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/post/" class="nav-link router-link-active">博客</a></div><div class="nav-item"><a href="https://github.com/jaykenxie" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/post/" class="nav-link router-link-active">博客</a></div><div class="nav-item"><a href="https://github.com/jaykenxie" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <!----> </aside> <main class="page"><div class="page-left"><div class="page-content"><div class="page-title"><h3>图解JS数组的forEach、map、find、reduce、filter方法</h3> <div class="page-title_info"><span class="page-title_author"><img src="https://avatars3.githubusercontent.com/u/29194135?v=4" alt class="page-title_avatar"> <span>谢小舜</span></span> <span class="page-title_time"><span>2019-10-10 02:55:36</span></span></div></div> <div class="theme-content content__default"><p>JavaScript数组 Array.prototype提供了几个非常方便的迭代方法，这里用图解的方式来理解这些方法。</p> <h2 id="array-foreach"><a href="#array-foreach" aria-hidden="true" class="header-anchor">#</a> Array.forEach</h2> <p>forEach() 方法对数组的每个元素执行一次提供的函数。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> array1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

array1<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">element</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// expected output: &quot;a&quot;</span>
<span class="token comment">// expected output: &quot;b&quot;</span>
<span class="token comment">// expected output: &quot;c&quot;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div> <p><img src="https://blog-1251280787.cos.ap-shanghai.myqcloud.com/undefined2019-10-10-2019062601-foreach.png" alt="2019-10-10-2019062601-foreach.png"></p> <h2 id="array-map"><a href="#array-map" aria-hidden="true" class="header-anchor">#</a> Array.map</h2> <p>map() 方法创建一个新数组，其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> array1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token comment">// pass a function to map</span>
<span class="token keyword">const</span> map1 <span class="token operator">=</span> array1<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> x <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>map1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// expected output: Array [2, 8, 18, 32]</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div> <p><img src="https://blog-1251280787.cos.ap-shanghai.myqcloud.com/undefined2019-10-10-2019062601-map.png" alt="2019-10-10-2019062601-map.png"></p> <h2 id="array-filter"><a href="#array-filter" aria-hidden="true" class="header-anchor">#</a> Array.filter</h2> <p>filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> words <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'spray'</span><span class="token punctuation">,</span> <span class="token string">'limit'</span><span class="token punctuation">,</span> <span class="token string">'elite'</span><span class="token punctuation">,</span> <span class="token string">'exuberant'</span><span class="token punctuation">,</span> <span class="token string">'destruction'</span><span class="token punctuation">,</span> <span class="token string">'present'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> result <span class="token operator">=</span> words<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token parameter">word</span> <span class="token operator">=&gt;</span> word<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// expected output: Array [&quot;exuberant&quot;, &quot;destruction&quot;, &quot;present&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div> <p><img src="https://blog-1251280787.cos.ap-shanghai.myqcloud.com/undefined2019-10-10-2019062601-filter.png" alt="2019-10-10-2019062601-filter.png"></p> <h2 id="array-find"><a href="#array-find" aria-hidden="true" class="header-anchor">#</a> Array.find</h2> <p>find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> array1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">130</span><span class="token punctuation">,</span> <span class="token number">44</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> found <span class="token operator">=</span> array1<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">element</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> element <span class="token operator">&gt;</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>found<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// expected output: 130</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div> <p><img src="https://blog-1251280787.cos.ap-shanghai.myqcloud.com/undefined2019-10-10-2019062601-find.png" alt="2019-10-10-2019062601-find.png"></p> <h2 id="array-reduce"><a href="#array-reduce" aria-hidden="true" class="header-anchor">#</a> Array.reduce</h2> <p>reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行)，将其结果汇总为单个返回值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> array1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token function-variable function">reducer</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">acc<span class="token punctuation">,</span> cur</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> acc <span class="token operator">+</span> cur<span class="token punctuation">;</span>

<span class="token comment">// 1 + 2 + 3 + 4</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>array1<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span>reducer<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// expected output: 10</span>

<span class="token comment">// 5 + 1 + 2 + 3 + 4</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>array1<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span>reducer<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// expected output: 15</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div> <p><img src="https://blog-1251280787.cos.ap-shanghai.myqcloud.com/undefined2019-10-10-2019062601-reduce.png" alt="2019-10-10-2019062601-reduce.png"></p></div> <div class="page-foot"><span><a href="/post/#JS">#JS</a></span><span><a href="/post/#Array">#Array</a></span></div></div> <div class="page-comment"><!----></div> <!----> </div> <div class="page-right"><div class="toc"><h4>目录</h4> <ul class="toc-wrap"><!----> <ul><li><a href="#array-foreach">Array.forEach</a></li></ul> <!----> <!----><!----> <ul><li><a href="#array-map">Array.map</a></li></ul> <!----> <!----><!----> <ul><li><a href="#array-filter">Array.filter</a></li></ul> <!----> <!----><!----> <ul><li><a href="#array-find">Array.find</a></li></ul> <!----> <!----><!----> <ul><li><a href="#array-reduce">Array.reduce</a></li></ul> <!----> <!----></ul></div></div></main> <footer class="footer" data-v-437632ea><div class="footer-ctx" data-v-437632ea><div class="footer-ctx-l" data-v-437632ea><h5 data-v-437632ea>自述</h5> <p data-v-437632ea>接触前端以来一直想自己设计开发一个博客，用于分想和记录工作、生活和学习中遇到的问题，与解决问题的思路和感悟。 (^o^)/~。</p></div> <div class="footer-ctx-m" data-v-437632ea><h5 data-v-437632ea>相关链接</h5> <ul class="flink" data-v-437632ea><li class="flink-wrap" data-v-437632ea><a href="/" class="dedline" data-v-437632ea>关于我</a></li><li class="flink-wrap" data-v-437632ea><a href="https://github.com/jaykenxie" class="dedline" data-v-437632ea>GitHub</a></li><li class="flink-wrap" data-v-437632ea><a href="https://weibo.com/autoshun" class="dedline" data-v-437632ea>微博</a></li></ul></div> <div class="footer-ctx-r" data-v-437632ea><img src="/images/logo_w.png" alt="Jayken (谢小舜)|谢小舜的个人博客" class="footer-ctx-r_logo" data-v-437632ea></div></div> <div class="footer-info" data-v-437632ea><a href="/" data-v-437632ea>©Jayken</a> <i data-v-437632ea> · </i> <a href="https://v1.vuepress.vuejs.org" data-v-437632ea>Vuepress</a>
      |  
    <a href="http://www.miitbeian.gov.cn/publish/query/indexFirst.action" target="_blank" class="dedline" data-v-437632ea>粤ICP备18012800号-2</a></div></footer></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.2d175a42.js" defer></script><script src="/assets/js/2.af4947b8.js" defer></script><script src="/assets/js/7.f9e4b783.js" defer></script>
  </body>
</html>
